<?php

/**
 * ECSHOP 手机网商品分类
 * ============================================================================
 * * 版权所有 2005-2012 上海商派网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.ecshop.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: liubo $
 * $Id: category.php 17217 2011-01-19 06:29:08Z liubo $
*/

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');
if ((DEBUG_MODE & 2) != 2)
{
    $smarty->caching = true;
}


/*------------------------------------------------------ */
//-- INPUT
/*------------------------------------------------------ */

/* 获得请求的分类 ID */
if (isset($_REQUEST['id']))
{
    $cat_id = intval($_REQUEST['id']);
}
elseif (isset($_REQUEST['category']))
{
    $cat_id = intval($_REQUEST['category']);
}
else
{
    /* 如果分类ID为0，则返回首页 */
    ecs_header("Location: ./\n");

    exit;
}


/* 初始化分页信息 */
$page = isset($_REQUEST['page'])   && intval($_REQUEST['page'])  > 0 ? intval($_REQUEST['page'])  : 1;
$size = 10;
/* 排序、显示方式以及类型 */
$default_sort_order_method = $_CFG['sort_order_method'] == '0' ? 'DESC' : 'ASC';
$default_sort_order_type   = $_CFG['sort_order_type'] == '0' ? 'goods_id' : ($_CFG['sort_order_type'] == '1' ? 'shop_price' : 'last_update');
$sort  = (isset($_REQUEST['sort'])  && in_array(trim(strtolower($_REQUEST['sort'])), array('click_count', 'shop_price', 'buy_sum'))) ? trim($_REQUEST['sort'])  : $default_sort_order_type;
$order = (isset($_REQUEST['order']) && in_array(trim(strtoupper($_REQUEST['order'])), array('ASC', 'DESC')))                              ? trim($_REQUEST['order']) : 'ASC';
$smarty->assign('url', "category.php?category=$cat_id&display=$display&brand=$brand&price_min=$price_min&price_max=$price_max&filter_attr=$filter_attr_str&page=$page&sort=$sort&order=$order");
/*------------------------------------------------------ */
//-- PROCESSOR
/*------------------------------------------------------ */
/* 页面的缓存ID */
$cache_id = sprintf('%X', crc32('mobile'.'-'.$cat_id . '-' . $sort  .'-' . $order  .'-' . $page . '-' . $size ));
if (!$smarty->is_cached('category.dwt', $cache_id))
{   

    /* 如果页面没有被缓存则重新获取页面的内容 */
    $children = get_children($cat_id);
    $cat = get_cat_info($cat_id);   // 获得分类的相关信息    
    if (!empty($cat))
    {
        $smarty->assign('keywords',    htmlspecialchars($cat['keywords']));
        $smarty->assign('description', htmlspecialchars($cat['cat_desc']));
        $smarty->assign('cat_style',   htmlspecialchars($cat['style']));
    }
    else
    {
        /* 如果分类不存在则返回首页 */
        ecs_header("Location: ./\n");

        exit;
    }
    /* 赋值固定内容 */
    assign_template('c', array($cat_id));
    $position = assign_ur_here($cat_id, $brand_name);//获取当前页面信息
    $count = get_cagtegory_goods_count($children);//统计总页面数
    $goodslist = category_get_goods($children,$size, $page,$sort,$order);//获取商品列表  
    $smarty->assign('page_title',       $position['title']);    // 页面标题
    $smarty->assign('top_title',       $cat['cat_name']);    // 页面标题
    $smarty->assign('category',         $cat_id);//分类ID 
    $smarty->assign('goods_list',       $goodslist);
    $smarty->assign('category',         $cat_id);
    $smarty->assign('script_name', 'category');
    assign_pager('category',            $cat_id, $count, $size, $sort, $order, $page, ''); // 分页
    assign_dynamic('category'); // 动态内容
    // 商品列表
    if($cat_id==101){
        exit;
    }else{

    }
}
    
    /* 获取产品列表底部广告 */
    $ad_1=get_ad(11);//产品列表底部广告
    $smarty->assign('ad_1', $ad_1);

    $page_address=$page<4?0:($page-3)*44;
    $smarty->assign('page_address', $page_address);
    $smarty->assign('page_now', $page);
    $smarty->assign('footer', get_footer());
    $smarty->display('category.dwt', $cache_id);


/*------------------------------------------------------ */
//-- PRIVATE FUNCTION
/*------------------------------------------------------ */

/**
 * 获得分类的信息
 *
 * @param   integer $cat_id
 *
 * @return  void
 */
function get_cat_info($cat_id)
{
    return $GLOBALS['db']->getRow('SELECT cat_name, keywords, cat_desc, style, grade, filter_attr, parent_id FROM ' . $GLOBALS['ecs']->table('category') .
        " WHERE cat_id = '$cat_id'");
}
/**
 * 获得分类下的商品总数
 *
 * @access  public
 * @param   string     $cat_id
 * @return  integer
 */
function get_cagtegory_goods_count($children, $brand = 0, $min = 0, $max = 0, $ext='')
{
    $where  = "g.warn_number = 1 AND g.is_on_sale = 1 AND g.is_alone_sale = 1 AND g.is_delete = 0 AND ($children OR " . get_extension_goods($children) . ')';

    if ($brand > 0)
    {
        $where .=  " AND g.brand_id = $brand ";
    }

    if ($min > 0)
    {
        $where .= " AND g.shop_price >= $min ";
    }

    if ($max > 0)
    {
        $where .= " AND g.shop_price <= $max ";
    }

    /* 返回商品总数 */
    return $GLOBALS['db']->getOne('SELECT COUNT(*) FROM ' . $GLOBALS['ecs']->table('goods') . " AS g WHERE $where $ext");
}
//产品列表
// $children     分类ID
// $size         显示大小
// $page         页码
// $sort         排序类型
// $order        排序方式，倒序，正序
function category_get_goods($children, $size, $page,$sort,$order)
{
    /* 获得商品列表 */
  
    $where = "g.warn_number = 1 AND g.is_on_sale = 1 AND g.is_alone_sale = 1 AND ".
        "g.is_delete = 0      AND ($children OR " . get_extension_goods($children) . ')';
    // 检测排序类型跟方式
    $order_by='ORDER BY  '.$sort.' '.$order;
    $sql = 'SELECT g.goods_id,g.shopall_note,g.goods_name, g.market_price, g.shop_price, g.preferential_price2, g.preferential_price5, g.goods_thumb, g.click_count, g.promote_price, g.promote_start_date, g.promote_end_date, g.is_new, ' . 
            "(select sum(goods_number) from " . $GLOBALS['ecs']->table('order_goods') . " WHERE goods_id = g.goods_id ) AS buy_sum " . 
            ' FROM ' . $GLOBALS['ecs']->table('goods').'AS g ' . 
            " WHERE $where $order_by ";
  
    $res = $GLOBALS['db']->selectLimit($sql, $size, ($page - 1) * $size);
    // 重组输出数组
    $arr = array();
    while ($row = $GLOBALS['db']->fetchRow($res))
    {  

        /* 判断是否存在限时特购价格 */
        if ($row['promote_price'] > 0)
        {
            $promote_price = bargain_price($row['promote_price'], $row['promote_start_date'], $row['promote_end_date']);
        }
        else
        {
            $promote_price = 0;
        }

        $arr[$row['goods_id']]['goods_id']             = $row['goods_id'];
        $arr[$row['goods_id']]['name']                 = !empty($row['shopall_note'])?$row['shopall_note']:$row['goods_name'];
        $arr[$row['goods_id']]['click_count']          = $row['click_count'];
        /* 市场价 */
        if($row['market_price'] > 0){//存在市场价时的市场价
            $arr[$row['goods_id']]['market_price']     = price_format($row['market_price']);
        }else{//不存在市场价时的市场价
            $arr[$row['goods_id']]['market_price']     = price_format(sprintf('%.1f',$row['shop_price']*1.2));
        }
        $arr[$row['goods_id']]['shop_price']           = price_format($row['shop_price']);
        $arr[$row['goods_id']]['preferential_price2']  = ($row['preferential_price2'] > 0) ? price_format($row['preferential_price2']) : ''; //第二梯度价格
        $arr[$row['goods_id']]['preferential_price5']  = ($row['preferential_price5'] > 0) ? price_format($row['preferential_price5']) : ''; //第五梯度价格
        $arr[$row['goods_id']]['promote_price']        = ($promote_price > 0) ? price_format($promote_price) : '';
        $arr[$row['goods_id']]['goods_thumb']          = get_image_path($row['goods_id'], $row['goods_thumb'], true);
        $arr[$row['goods_id']]['url']                  = build_uri('goods', array('gid'=>$row['goods_id']), $row['goods_name']);
        $arr[$row['goods_id']]['is_new']               = $row['is_new'];

    }
    return $arr;
}

/**
 * 获得广告位
 *
 * @access  private
 * @return  array
 */
function get_ad($position_id = 0){
        if(!$position_id) return false;
        $position_id = intval($position_id);
        $sql = "SELECT * FROM " . $GLOBALS['ecs']->table('ad') . " WHERE position_id = '$position_id' ORDER BY ad_name ASC ";
        return $GLOBALS['db']->getAll($sql);
    }

?>
